﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC.Music.Data.Models;
using System.Threading.Tasks;

namespace MVC.MusicCMS.Web.Controllers
{
    public class TrackController : BaseController
    {
        //
        // GET: /Track/
        public TrackController()
		{
			ThisControllerLivesInWebsiteSection( "track");
		}
        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to kick-start your ASP.NET MVC application.";
           // return View(_trackRepository.Query());
            return View(_trackRepository.Query());
        }

        //
        // GET: /Track/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Track/Create

        public ActionResult Create()
        {
            var track = new TrackModel();
           // List<object> values = new List<object>();
           // values.Add(new { ID = "choose", Name = "--Select--" });
            //values.AddRange(_database.Query<BitrateModel>("select * from tbl_Bitrate", null).Select(b=>new {ID = b.BitrateID, Name = b.Value}));

            ViewData["bitrates"] = _database.Query<BitrateModel>("select * from tbl_Bitrate", null).ToList();// new SelectList(values, "ID", "Name");
          
            return View(track);
        }

        //
        // POST: /Track/Create

        [HttpPost]
        public ActionResult Create(TrackModel track)
        {
            try
            {
                _database.Insert(track);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Track/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Track/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Track/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Track/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public JsonResult GetBitrates()
        {
            var listBitrate = new List<SelectListItem>();

            Parallel.ForEach(_bitrateRepository.Query(), b =>
                listBitrate.Add(new SelectListItem()
                {
                    Selected = true,
                    Text = b.Name,
                    Value = b.BitrateID.ToString()

                }));
            return Json(listBitrate, JsonRequestBehavior.AllowGet);
        }
    }
}
